//
//  HomeVC+Photo.swift
//  WatermarkCamera
//
//  Created by 赵新 on 5/7/2024.
//

import UIKit
import AxcBedrock

extension HomeVC {
    /// 拍照模式
    func photoMode() {
        let functionMode: HomeModeView.Mode = .photo
        vm.functionMode = functionMode
        // 顶部
        headerView.switchMode(functionMode, cameraView: cameraView)
        menuView.switchMode(functionMode)
        // 中部
        switchMode(functionMode)
        // 底部
        footerView.switchMode(functionMode)
    }

    /// 点击拍照
    func clickTakePhoto() {
        let cameraStatus = AuthorizedManager.camera()
        let photoAlbumStatus = AuthorizedManager.photoAlbum()
        if cameraStatus.info.isAvailable {
            if photoAlbumStatus.info.isAvailable {
                takePhotoAction()
            } else {
                AuthorizedManager.showPrivacyAlert(type: .photoAlbum)
            }
        } else {
            AuthorizedManager.showPrivacyAlert(type: .camera)
        }
    }

    /// 拍照
    func takePhotoAction() {
        // 检查磁盘空间
        let freeSize = UIDevice.Axc.FreeDiskSpace_MB ?? 0
        guard freeSize > 10 else { // 磁盘空间不足
            let alert = AlertView()
            alert.title = "空间不足"
            alert.msg = "手机存储空间不足，请及时清理"
            alert.style = .confirm(title: "我知道了")
            alert.show()
            return
        }
        // 禁止息屏
        UIApplication.shared.isIdleTimerDisabled = true
        // 锁定操作
        lockAction()
        // 停止虚线闪烁
        controlView.stopFlashDottedLayer()
        // 获取是哪里来的延时
        var _currentDelayTime = 0
        switch vm.functionMode {
        case .photo:
            let delayBtn: DelayBtn = menuView.delayBtn
            _currentDelayTime = delayBtn.delayTime.info.time
        default: break
        }
        // 进行拍照动画
        controlView.setDelayTakePhotoAnimation(_currentDelayTime)
        // 进行延迟拍摄
        startDelayTakePhoto(_currentDelayTime)
    }

    /// 开始拍照
    func startDelayTakePhoto(_ time: Int) {
        perform(#selector(self.takePhoto), with: nil, afterDelay: TimeInterval(time))
    }

    /// 拍照方法
    @objc
    func takePhoto() {
        // 拍照闪一下动画
        takePhotoAnimation()
        // 延时/直拍结束
        cameraView?.collectPhoto { [weak self] image in
            guard let weakSelf = self else { return }

            switch weakSelf.vm.functionMode {
            // 常规拍照
            case .photo:
                // 进行处理图片
                weakSelf.vm.fotoMix(image, watermarkContentView: weakSelf.watermarkContentView) {
                    // 允许息屏
                    AxcGCD.Main {
                        UIApplication.shared.isIdleTimerDisabled = false
                    }
                    // 开放拍照按钮
                    weakSelf.unlockAction()
                }
            default: break
            }
        }
    }

    /// 取消拍照
    func cancelDelayTakePhoto() {
        unlockAction()
        controlView.cancelDelayTakePhotoAnimation()
        HomeVC.cancelPreviousPerformRequests(withTarget: self, selector: #selector(self.takePhoto), object: nil)
    }

    /// 解锁锁定操作
    func lockAction() {
        AxcGCD.Main { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.lockActionView.isHidden = false
        }
    }

    /// 解锁锁定操作
    func unlockAction() {
        AxcGCD.Main { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.lockActionView.isHidden = true
        }
    }

    /// 拍照动画
    func takePhotoAnimation() {
        footerView.actionBtn.transform = CGAffineTransform.Axc.CreateScale(1)
        SpringAnimation(withDuration: 0.2) { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.footerView.actionBtn.transform = CGAffineTransform.Axc.CreateScale(0.8)
        } completion: {
            SpringAnimation(withDuration: 0.2) { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.footerView.actionBtn.transform = CGAffineTransform.Axc.CreateScale(1)
            }
        }
        controlView.backgroundColor = .clear
        SpringAnimation(withDuration: 0.1) { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.controlView.backgroundColor = .white.axc.alpha(0.9)
        } completion: {
            SpringAnimation(withDuration: 0.1) { [weak self] in
                guard let weakSelf = self else { return }
                weakSelf.controlView.backgroundColor = .clear
            }
        }
    }
}
